//
//  Tree.m
//  Tree
//
//  Created by Roeru on 10/7/14.
//  Copyright (c) 2014 Shinoi. All rights reserved.
//

#import "Tree.h"

@implementation Tree


- (void) setTree
{
    struct Num{
        int a;
        struct Num *nextLeft;
        struct Num *nextRight;
    };
    typedef struct Num listNum;
    
    
    listNum *root;
//    listNum *pL = NULL;
//    listNum *pR = NULL;
 
    listNum *mov;
    listNum *p;
    
    root = (listNum *)malloc(sizeof(listNum));
    
    (*root).a = 1;
    (*root).nextLeft = NULL;
    (*root).nextRight = NULL;
    
    NSLog(@"root = %d",(*root).a);

    int i = 1;
    int b = 1;
    while (i)
    {
        
//        mov = (listNum *)malloc(sizeof(listNum));
        p = (listNum *)malloc(sizeof(listNum));
        
        if (NULL == (*root).nextLeft) {
            
            b = b + 1;
            (*p).a = b;
            (*p).nextLeft = NULL;
            (*p).nextRight = NULL;
            
            (*root).nextLeft = p;
            
            NSLog(@"rootL = %d",(*p).a);
            continue;
        }
        
        if (NULL == (*root).nextRight) {
            
            b = b + 1;
            (*p).a = b;
            (*p).nextLeft = NULL;
            (*p).nextRight = NULL;
            
            (*root).nextRight = p;
            
            NSLog(@"rootR = %d",(*p).a);
            continue;
        }
        
//        p = (listNum *)malloc(sizeof(listNum));
        
        if (NULL != (*root).nextLeft)
        {
            mov = (*root).nextLeft;
            if (NULL == (*mov).nextLeft) {
                
                
                b = b + 1;
                (*p).a = b;
                (*p).nextLeft = NULL;
                (*p).nextRight = NULL;
                
                (*mov).nextLeft = p;
                
                NSLog(@"rootLL = %d",(*p).a);
                continue;
            }
            
            if (NULL == (*mov).nextRight) {
                
                
                b = b + 1;
                (*p).a = b;
                (*p).nextLeft = NULL;
                (*mov).nextRight = NULL;
                
                (*mov).nextRight = p;
                
                NSLog(@"rootLR = %d",(*p).a);
                continue;
            }

        }
        
        if (NULL != (*root).nextRight)
        {
            mov = (*root).nextRight;
            if (NULL == (*mov).nextLeft) {
                
                
                b = b + 1;
                (*p).a = b;
                (*p).nextLeft = NULL;
                (*p).nextRight = NULL;
                
                (*mov).nextLeft = p;
                
                NSLog(@"rootRL = %d",(*p).a);
                continue;
            }
            
            if (NULL == (*mov).nextRight) {
                
                
                b = b + 1;
                (*p).a = b;
                (*p).nextLeft = NULL;
                (*p).nextRight = NULL;
                
                (*mov).nextRight = p;
                
                NSLog(@"rootRR = %d",(*p).a);
                continue;
            }
        
        }
        i = 0;
    }
    
}
////////////////////////////////////////////////////////////////
//    if (NULL  == (*root).nextLeft) {
//        (*root).nextLeft = (listNum *)malloc(sizeof(listNum));
//        (*(*root).nextLeft).nextLeft = NULL;
//        (*(*root).nextLeft).nextRight = NULL;
//    }
//    
//    if (NULL == (*root).nextRight) {
//        (*root).nextRight = (listNum *)malloc(sizeof(listNum));
//        (*(*root).nextRight).nextLeft = NULL;
//        (*(*root).nextRight).nextRight = NULL;
//    }
//    
//    
//    
//////////////////////////////////////////////////////////
//    if (NULL == (*mov).nextLeft) {
//        NSLog(@"please enter the next number");
//        scanf("%d",&(*p).a);
//        (*mov).nextLeft = p;
//        mov = p;
//    }
//    
//    if (NULL == (*mov).nextRight) {
//        NSLog(@"please enter the next number");
//        scanf("%d",&(*p).a);
//        (*mov).nextRight = p;
//        mov = p;
//    }
//////////////////////////////////////////////////////////
//    pL = (listNum *)malloc(sizeof(listNum));
//    (*root).nextLeft = pL;
//    (*pL).nextLeft = NULL;
//    (*pL).nextRight = NULL;
//    
//    pR = (listNum *)malloc(sizeof(listNum));
//    (*root).nextRight = pR;
//    (*pR).nextLeft = NULL;
//    (*pR).nextRight = NULL;
//    
//    
//    listNum *pLL = NULL;
//    listNum *pLR = NULL;
//    listNum *pRR = NULL;
//    listNum *pRL = NULL;
//    
//    pLL = (listNum *)malloc(sizeof(listNum));
//    (*pL).nextLeft = pLL;
//    (*pLL).nextLeft = NULL;
//    (*pLL).nextRight = NULL;
//    
//    pLR = (listNum *)malloc(sizeof(listNum));
//    (*pL).nextRight = pLR;
//    (*pLR).nextLeft = NULL;
//    (*pLR).nextRight = NULL;
//    
//    pRL = (listNum *)malloc(sizeof(listNum));
//    (*pR).nextLeft = pRL;
//    (*pRL).nextLeft = NULL;
//    (*pRL).nextRight = NULL;
//    
//    pRR = (listNum *)malloc(sizeof(listNum));
//    (*pR).nextRight = pRR;
//    (*pRR).nextLeft = NULL;
//    (*pRR).nextRight = NULL;
//////////////////////////////////////////////////////////


@end
    
